import { combineReducers } from '@reduxjs/toolkit';
import { persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // 默认使用localStorage
import draftsReducer from './draftsSlice';
import selectedSegmentReducer from './selectedSegmentSlice';
import timelineReducer from './timeline';
import segmentFormReducer from '../components/form/v2/store';

// 定义需要持久化的状态
const rootReducer = combineReducers({
    drafts: draftsReducer,
    selectedSegment: selectedSegmentReducer,
    timeline: timelineReducer,
    segmentForm: segmentFormReducer,
});

// 持久化配置
const persistConfig = {
    key: 'root',
    storage,
    // 不持久化tracks状态，确保每次刷新都使用初始固定轨道
    // 不持久化tracks、drafts和素材相关状态
    // 不持久化timeline状态，确保每次刷新游标位置重置
    // 不持久化segmentForm状态，确保每次刷新配置重置
    blacklist: ['tracks', 'drafts', 'videos', 'audios', 'texts', 'stickers', 'timeline', 'segmentForm'],
};

// 创建持久化reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

export default persistedReducer;

export type RootState = ReturnType<typeof rootReducer>;
